package com.cy.letcode.link.reverse;

/**
 * 链表反转
 * head->1->2->3
 * head->3->2->1
 * 参考图解
 * https://www.cnblogs.com/lixinjie/p/a-post-help-you-understanding-sinlge-linkedlist-and-never-forget.html
 *
 * @author yuchen
 */
public class LinkedReverse {

    public ListNode reversLink(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode doing = head;
        ListNode done = null;
        ListNode todo = null;

        while (doing != null) {
            todo = doing.next;
            doing.next = done;
            done = doing;
            doing = todo;
        }
        return done;
    }

    public static void main(String[] args) {
        ListNode l = new ListNode(1, new ListNode(2, new ListNode(3)));
        ListNode listNode = new LinkedReverse().reversLink(l);
        while (listNode != null) {
            System.out.print(listNode.val);
            listNode = listNode.next;
        }
    }
}
